/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.calculator;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.context.ReportContext;
import com.reportgear.report.model.ReportModel;
import com.reportgear.report.model.SalaryReportModel;
import com.reportgear.report.model.cell.value.salary.SalaryObjectType;

/**
 * 计算参数
 * 
 * @version 1.0 2011-6-16
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class CalcParameter {
	// 工资对象
	private Set<Integer> objectSet = Collections.emptySet();
	// 选中的工资对象
	private Set<Integer> selectObjSet = Collections.emptySet();
	// 报表上下文
	private ReportContext ctx = ReportContext.INSTANCE;
	// 最高水平
	private int maxLevel = 0;
	// Session信息
	private SessionInfo sessionInfo;

	public void calculate(ReportModel model) {
		for (int lvl : ctx.getShowLevelList()) {
			this.maxLevel = Math.max(this.maxLevel, lvl);
		}

		if (ctx.isUseSelectedObject()) {
			this.selectObjSet = new HashSet<Integer>();
			this.selectObjSet.add(ctx.getSelectedObjectId());
		}

		if (model instanceof SalaryReportModel) {
			// 是否过滤判断
			List<SalaryDataObject> dataList;
			if (ctx.isUseSelectedObject()) {
				dataList = SalaryHelper.INSTANCE.getPayObjects(ctx.getPaymentId(), this.getSalaryObjectType(),
						this.maxLevel, this.selectObjSet, Collections.<Integer> emptySet());
			} else {
				dataList = SalaryHelper.INSTANCE.getPayObjects(ctx.getPaymentId(), this.getSalaryObjectType(),
						this.maxLevel, Collections.<Integer> emptySet(), Collections.<Integer> emptySet());
			}

			this.objectSet = new HashSet<Integer>();
			for (SalaryDataObject sdo : dataList) {
				this.objectSet.add(sdo.getInt("id"));
			}
		}

		this.sessionInfo = new SessionInfo();
		SalaryDataObject sdo = SalaryHelper.INSTANCE.getSessionVar();
		this.sessionInfo.setBranchName(sdo.getString("LoginBranchName"));
		this.sessionInfo.setOrganization(sdo.getString("LoginOrgnizationName"));
		this.sessionInfo.setPosition(sdo.getString("LoginPositionName"));
		this.sessionInfo.setOperator(sdo.getString("PersonName"));
	}

	public SessionInfo getSessionInfo() {
		return this.sessionInfo;
	}

	public Set<Integer> getObjectSet() {
		return objectSet;
	}

	/**
	 * 
	 * @return 发放表Id
	 */
	public int getPaymentId() {
		return ctx.getPaymentId();
	}

	/**
	 * 
	 * @return 分组水平
	 */
	public int getGroupLevel() {
		return ctx.getGroupLevel();
	}

	/**
	 * 
	 * @return 显示层级
	 */
	public List<Integer> getShowLevelList() {
		return Collections.unmodifiableList(ctx.getShowLevelList());
	}

	/**
	 * 
	 * @return 最高水平
	 */
	public int getMaxLevel() {
		return maxLevel;
	}

	/**
	 * 
	 * @return 选中的对象
	 */
	public Set<Integer> getSelectObjSet() {
		return Collections.unmodifiableSet(selectObjSet);
	}

	/**
	 * @return 薪酬项目类型
	 */
	public SalaryObjectType getSalaryObjectType() {
		return ctx.getSalaryObjectType();
	}
}
